package com.smscrypt;

import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;

import android.app.Activity;
import android.content.Intent;


/** QR-Module
 * Module which has the Task to scan, process and share the QR-Code to distribute secretkeys
 * for the SMS-Encryption (use for other application not prohibited)<br />
 * Also see method Descriptions<br />
 * {@link #share(String[])}<br />
 * {@link #prozessScan(String)}<br />
 * {@link #scan()}<br />
 * {@link #scanResult(int, int, Intent)}<br />
 * {@link #toString()}<br />
 * 
 * 
 * @author Julian
 * @version 1.0
 *
 */

public class QR {
	private Activity activity;
	private String[] scanString;
	
	public QR(Activity activity){
		this.activity = activity;
	}
	
	public void scan(){
		// Calls the 
		IntentIntegrator.initiateScan(activity);
	}
	
	/**share()
	 * <p>Builds a QR-Code out of a String Array</p>
	 * 
	 * @param input takes a String Array {Firstname, Lastname, Phonenumber, Secret key}
	 */
	
	public void share(String[] input){
		/**
		 * parsing input
		 */
		StringBuilder sb = new StringBuilder();
		String name = input[1];
		String fname = input[0];
		String tel = input[2];
		String memo = input[3];
		
		/**
		 * Build the Card to read
		 */
		sb.append("MECARD:N:");
		sb.append(fname);
		sb.append(" ");
		sb.append(name);
		sb.append(";TEL:");
		sb.append(tel);
		sb.append(";NOTE:");
		sb.append(memo);
		sb.append(";");
		
		/*
		 * Starts the output of the QR-Code
		 */
		IntentIntegrator.shareText(activity, sb.toString());
	}
	
	/**<h4>prozessString()</h4>
	 * <p>Analysis the String which was scanned by the QR Reader</p>
	 * 
	 * @param txt the text which is checkt on right contruction. 
	 * @return a Array of Strings in the right format of {Firstname, Lastname, Phonenumber, Secretkey} / In case of a false input combination it returns null
	 */
	public String[] prozessScan(String txt){
		/**
		 * Split the String in the 4 Parts (If they are more we do not Care yet)
		 */
		String[] splitTxt = txt.split(";");
		String[] names = splitTxt[0].split(":");
		String[] phone = splitTxt[1].split(":");
		String[] memo = splitTxt[2].split(":");
		
		/**
		 * Check if the First part of the Array are the Types we expect
		 * if not then throw a null Array
		 */
		if(names[0].equals("MECARD") && names[1].equals("N") && phone[0].equals("TEL") && memo[0].equals("NOTE")){
			String[] name = names[2].split(" ");
			return new String[] {name[0],name[1],phone[1], memo[1]};
		}
		/**
		 * If not processed probably then return a fake String[]
		 */
		return new String[] {"Max", "Mustermann", "+49666", "SecretKey"};
	}
	
	/**<h4>{@link #scanResult(int, int, Intent)}</h4>
	 * <p><b>Muat be placed in the onActivityResult(int,int,intent) Block in the Activity where {@link #scan()} is
	 * called.</b> It handles the result of the scanned qr-code and stores the result in a String[] called scanString
	 * You can get these value with the {@link #getScanString()}
	 * 
	 * @param requestCode
	 * @param resultCode
	 * @param intent
	 */
	
	public void scanResult(int requestCode, int resultCode, Intent intent) {
		IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
 	   if (scanResult != null) {
 	     scanString = prozessScan(scanResult.getContents());
 	   } else {
 	   // else continue with any other code you need in the method
 	   }
	}
	
	/**<h4>{@link #getScanString()}</h4>
	 * <p>Only returns the {@link #scanString} which was prozessed by the {@link #scanResult(int, int, Intent)} and 
	 * {@link #prozessScan(String)}
	 * 
	 * @return a String[] with the Data of the QR-object.
	 */
	public String[] getScanString(){
		return scanString;
	}

	/**<h4>{@link #toString()}</h4>
	 * <p>There is nothing toString so it returns nothing :D
	 * 
	 */
	public String toString(){
		return "Nothing toString here ;)";
	}
}
